/*
 * This file is Copyright 2002-2006 Loyola University Chicago,
 * Department of Computer Science and the Emerging Technologies Laboratory.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License via
 *    http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.citep.web.admin.accounts.server;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.BeanUtils;

import com.citep.business.*;
import com.citep.web.gwt.dto.*;
import com.citep.business.sessions.TransactionSession;;

public class DTOUtils {
	/* 
	 * Copies and converts an account object's properties to its dto equivalent
	 */
	static public void accountToDTO(final Account source, AccountDTO dest) {
		BeanUtils.copyProperties(source, dest, new String [] {"startingBalance","currentBalance","type","startPeriod","endPeriod","accountCategory","lastModified"});

		//when we copy the properties to the DTO, we have to ignore the "balance" property,
		//since GWT does not support BigDecimal for the client side. Instead we use a string with the value.
		//Same thing for type, we can't copy an enum to a string
		//start period and end period, we need to convert them from TimeStamp (hibernate's fault) to Date
		
		dest.setCurrentBalance(source.getCurrentBalance().toString());
		
		if(source.getStartPeriod() != null)
			dest.setStartPeriod(new Date(source.getStartPeriod().getTime()));
		
		if(source.getEndPeriod() != null)
			dest.setEndPeriod(new Date(source.getEndPeriod().getTime()));
		
		if(source.getLastModified() != null)
			dest.setLastModified(new Date(source.getLastModified().getTime()));
		
		dest.setType(source.getType().toString());
		
		//copy the account category DTO
		AccountCategoryDTO accountCategory = new AccountCategoryDTO();
		BeanUtils.copyProperties(source.getAccountCategory(), accountCategory);
		
		dest.setAccountCategory(accountCategory);
		
	}
	
	//instantiates a DTO account for you
	static public AccountDTO accountToDTO(final Account source) {
		AccountDTO dto = new AccountDTO();
		accountToDTO(source, dto);
		return dto;
	}
	
	/* 
	 * Copies and converts an account DTO's properties to its account equivalent
	 * 
	 * Note that the accountCategory field will always be null
	 */
	static public void DTOtoAccount(final AccountDTO source, Account dest) {
		
		BeanUtils.copyProperties(source, dest, new String [] {"startingBalance","currentBalance","type","accountCategory"});
		
		dest.setCurrentBalance(new BigDecimal(source.getCurrentBalance()));
		dest.setType(AccountTypes.valueOf(source.getType().toUpperCase()));
	}
	
	

	static public void TransactionToDTO(final Transaction source, TransactionDTO dest) {
		BeanUtils.copyProperties(source, dest, new String [] {"user","entries","transactionTime","systemTime"});
		dest.setTransactionTime(new Date(source.getTransactionTime().getTime()));
		dest.setSystemTime(new Date(source.getSystemTime().getTime()));
	}
	
	static public void DTOtoTransaction(final TransactionDTO source, Transaction dest) {
		BeanUtils.copyProperties(source, dest);
	}
	
	static public void EntryToDTO(final Entry source, EntryDTO dest) {
		BeanUtils.copyProperties(source, dest, new String [] {"account","transaction","type","amount"});
		dest.setAccountId(source.getAccount().getId());
		dest.setTransactionId(source.getTransaction().getId());
		dest.setType(source.getType().toString());
		dest.setAmount(source.getAmount().toString());
		
		if(source.getSource() == null)
			dest.setSourceAccountId(0);
		else
			dest.setSourceAccountId(source.getSource().getId());
	}
	
	static public void DTOtoUser(final UserDTO source, User dest) {
		BeanUtils.copyProperties(source,dest, new String [] {"roleName"});
		//TODO: save the role as well
		
	}

	static public void UserToDTO(final User source, UserDTO dest) {
		BeanUtils.copyProperties(source, dest, new String [] {"role"});
		dest.setRoleName(source.getRole().getName());
	}

	static public void RoleToDTO(final Role source, RoleDTO dest) {
		BeanUtils.copyProperties(source, dest);
	}



}
